//package com.app.csc.fragment.my.exchangepoints;
//
//import android.os.Bundle;
//import android.text.Editable;
//import android.text.TextWatcher;
//import android.util.Log;
//import android.view.Gravity;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.AdapterView;
//import android.widget.ListView;
//
//import androidx.annotation.Nullable;
//import androidx.databinding.DataBindingUtil;
//import androidx.lifecycle.Observer;
//import androidx.lifecycle.ViewModelProviders;
//
//import com.app.csc.BR;
//import com.app.csc.R;
//import com.app.csc.app.AppViewModelFactory;
//import com.app.csc.databinding.FragmentExchangepointsBinding;
//import com.app.csc.entity.AreaDto;
//import com.app.csc.entity.ExchangeHomeEntity;
//import com.app.csc.fragment.my.exchangepoints.exchangepointsrecord.ExchangePointsRecordFragment;
//import com.app.csc.fragment.my.exchangepoints.exchangepointsrecord.ExchangePointsRecordTabFragment;
//import com.app.csc.utils.GlideUtils;
//import com.app.csc.utils.TextUtil;
//import com.app.csc.utils.ToastUtil;
//import com.app.csc.view.CustomPopWindow;
//import com.app.csc.view.ExchangeHomeAdapter;
//import com.app.csc.view.InputPwdDialog;
//import com.app.csc.view.ShopTypeWindows;
//import com.app.csc.view.WalletTypesAdapter;
//
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import me.goldze.mvvmhabit.base.BaseFragment;
//
///**
// * @author: woking
// * @date: 2021/6/9
// * @description：
// */
//public class ExchangePointsFragment extends BaseFragment<FragmentExchangepointsBinding, ExchangePointsViewModel> {
//
//    private ExchangeHomeEntity.Coin selectCoinLeft;
//    private ExchangeHomeEntity.Coin selectCoinRight;
//    private double currHuiLv = 1;
//    private double shouXuFei = 0;
//    private boolean isSelectInput = true;
//    @Override
//    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return R.layout.fragment_exchangepoints;
//    }
//
//    @Override
//    public int initVariableId() {
//        return BR.viewModel;
//    }
//
//    @Override
//    public ExchangePointsViewModel initViewModel()
//    {
//        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
//        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
//        return ViewModelProviders.of(this, factory).get(ExchangePointsViewModel.class);
//    }
//
//    @Override
//    public void initData() {
//        super.initData();
//
//        viewModel.exchangeHome();
//        viewModel.getShopToken();
//
//        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                getActivity().finish();
//            }
//        });
//        binding.tvTitleRight.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                startContainerActivity(ExchangePointsRecordFragment.class.getCanonicalName());
//            }
//        });
//        binding.adLl.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                initPOP(view,null);
//            }
//        });
//        binding.jifenLl.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                initPOP(view,selectCoinLeft);
//            }
//        });
//        binding.tvConfirm.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                String amount = binding.inputNumEt.getText().toString();
//                if(TextUtil.isEmpty(amount))
//                {
//                    ToastUtil.showToast("请输入兑换金额");
//                    return;
//                }
//                getMemberBaseInfo();
//            }
//        });
//
//        viewModel.mExchangeHomeEntity.observe(this, new Observer<ExchangeHomeEntity>() {
//            @Override
//            public void onChanged(ExchangeHomeEntity exchangeHomeEntity) {
//                selectCoinLeft = initPopData(exchangeHomeEntity, null,"AD").get(0);
//                Log.e("TAG",selectCoinLeft.coin_name+"=====AD=====0000=======积分=======");
//                selectCoinRight = initPopData(exchangeHomeEntity, null,"积分").get(0);
//                Log.e("TAG",selectCoinLeft.coin_name+"=====AD============积分======="+selectCoinRight.coin_name);
//                setViewText();
//            }
//        });
//
//        binding.inputNumEt.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//            @Override
//            public void afterTextChanged(Editable editable) {
////                Log.e("TAG",binding.inputNumEt.hasFocus()+"==============afterTextChanged============"+binding.outputNumEt.hasFocus());
//                if(binding.inputNumEt.hasFocus())
//                {
//                    double num = Double.parseDouble(editable.toString());
//                    double duihuan = (num * currHuiLv) * (1-shouXuFei);
//                    binding.outputNumEt.setText(duihuan+"");
//                }
//            }
//        });
//        binding.outputNumEt.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//            @Override
//            public void afterTextChanged(Editable editable) {
//                if(binding.outputNumEt.hasFocus())
//                {
//                    double num = Double.parseDouble(editable.toString());
//                    double duihuan = (num / ((1-shouXuFei)* currHuiLv));
//                    binding.inputNumEt.setText(duihuan+"");
//                }
//            }
//        });
//    }
//
//    private List<ExchangeHomeEntity.Coin> initPopData(ExchangeHomeEntity entity, ExchangeHomeEntity.Coin selectCoin, String coinName)
//    {
//        List<ExchangeHomeEntity.Coin> coins_result = new ArrayList<>();
//        if(null != entity && null != entity.coins && entity.coins.size() > 0)
//        {
//            List<ExchangeHomeEntity.Coin> coins = entity.coins;
//            for(int i = 0; i < coins.size(); i++)
//            {
//                ExchangeHomeEntity.Coin coin = coins.get(i);
//
//                if(!TextUtil.isEmpty(coinName))
//                {
//                    if(coinName.equals(coin.coin_name))
//                    {
//                        coins_result.add(coin);
//                        break;
//                    }
//                    else
//                    {
//                        continue;
//                    }
//                }
//
//                if(null != selectCoin)
//                {
//                    if(selectCoin.coin_name.equals(coin.coin_name))
//                    {
//                        continue;
//                    }
//                }
//                Map<String, Object> exchange = coin.exchange;
////                boolean isOpen = false;
//                int index = 0;
//                for (Map.Entry<String, Object> entry : exchange.entrySet())
//                {
//                    index++;
//                    if(!entry.getValue().equals("0.0000") && index != coin.id)
//                    {
////                        isOpen = true;
//                        coins_result.add(coin);
//                        break;
//                    }
//                    System.out.println(coin.coin_name + " Key = " + entry.getKey() + ", Value = " + entry.getValue());
//                }
//            }
//        }
//        return coins_result;
//    }
//
//    private InputPwdDialog inputPasswordDialog;
//    private void getMemberBaseInfo() {
//        inputPasswordDialog = new InputPwdDialog(getContext(), new InputPwdDialog.InputPasswordListener() {
//            @Override
//            public void callbackPassword(String password) {
//                Log.e("TAG","====states====callbackPassword=====password=="+password);
//                HashMap<String, Object> map = new HashMap<>();
//                map.put("amount",binding.inputNumEt.getText().toString());
//                map.put("coin_id",selectCoinLeft.id);
//                map.put("to_coin_id",selectCoinRight.id);
//                map.put("payment_password",password);
//                viewModel.exchange(map);
//            }
//        });
//        inputPasswordDialog.setOnDismissListener(dialog -> {
//
//        });
//        inputPasswordDialog.show();
//    }
//    private CustomPopWindow mCustomPopWindow;
//    private void initPOP(View view,ExchangeHomeEntity.Coin selectCoin)
//    {
//        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.pop_duihuan_leixing,null);
//
//        ListView listView = contentView.findViewById(R.id.listview_list);
//        ExchangeHomeAdapter adapter = new ExchangeHomeAdapter(getContext(),initPopData(viewModel.mExchangeHomeEntity.getValue(), selectCoin,null) );
//        listView.setAdapter(adapter);
//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
////                Log.e("TAG", "=======initPOP========"+ (selectCoin == null));
//                if(selectCoin == null)
//                {
//                    selectCoinLeft = (ExchangeHomeEntity.Coin)adapterView.getAdapter().getItem(i);
////                    Log.e("TAG",selectCoinLeft.coin_name+"=============00000============"+selectCoinRight.coin_name);
//                    if(selectCoinLeft.coin_name.equals(selectCoinRight.coin_name))
//                    {
////                        Log.e("TAG",selectCoinLeft.coin_name+"=============11111============"+selectCoinRight.coin_name);
//                        for(int j = 0; j< adapterView.getAdapter().getCount(); j++)
//                        {
//                            ExchangeHomeEntity.Coin coin = (ExchangeHomeEntity.Coin)adapterView.getAdapter().getItem(j);
////                            Log.e("TAG",selectCoinLeft.coin_name+"=============" + j+"============"+coin.coin_name);
//                            if(!selectCoinLeft.coin_name.equals(coin.coin_name))
//                            {
//                                selectCoinRight = (ExchangeHomeEntity.Coin)adapterView.getAdapter().getItem(j);
//                                break;
//                            }
//                        }
//                    }
//                }
//                else
//                {
//                    selectCoinRight = (ExchangeHomeEntity.Coin)adapterView.getAdapter().getItem(i);
//                    if(selectCoinLeft.coin_name.equals(selectCoinRight.coin_name))
//                    {
//                        for(int j = 0; j< adapterView.getAdapter().getCount(); j++)
//                        {
//                            ExchangeHomeEntity.Coin coin = (ExchangeHomeEntity.Coin)adapterView.getAdapter().getItem(j);
//                            if(!selectCoinRight.coin_name.equals(coin.coin_name))
//                            {
//                                selectCoinLeft = (ExchangeHomeEntity.Coin)adapterView.getAdapter().getItem(j);
//                                break;
//                            }
//                        }
//                    }
//                }
//                setViewText();
//                mCustomPopWindow.dissmiss();
//            }
//        });
//
//        //创建并显示popWindow
//        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(getContext())
//                .setView(contentView)
//                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
//                .setBgDarkAlpha(0.3f) // 控制亮度
//                .setFocusable(true)
//                .setOutsideTouchable(true)
////                .size(600,400) //设置显示的大小，不设置就默认包裹内容
//                .size(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
//                .create()
//                .showAtLocation(view, Gravity.BOTTOM,0,20);
////                .showAsDropDown(view,0,20);
//    }
//
//    private void setViewText()
//    {
//        if(selectCoinLeft != null && selectCoinRight != null)
//        {
//            binding.LeftCoinNameTv.setText(selectCoinLeft.coin_name);
//            binding.rightCoinNameTv.setText(selectCoinRight.coin_name);
//            GlideUtils.getInstances().loadNormalImg(getContext(),binding.LeftImg,selectCoinLeft.image);
//            GlideUtils.getInstances().loadNormalImg(getContext(),binding.rightImg,selectCoinRight.image);
//            binding.selectTypeTv.setText(selectCoinLeft.coin_name+"余额");
//            binding.selectYueTv.setText(selectCoinLeft.money);
//            binding.huilvTv.setText("1 "+selectCoinLeft.coin_name+" = "+ getHuiLv() + " " +selectCoinRight.coin_name);
//            binding.shouxufeiTv.setText((Double.parseDouble(selectCoinLeft.exchange_fee)*100) + "%");
//            shouXuFei = Double.parseDouble(selectCoinLeft.exchange_fee);
//        }
//    }
//
//    private double getHuiLv()
//    {
//        if(selectCoinLeft != null && selectCoinRight != null)
//        {
//            for (Map.Entry<String, Object> entry : selectCoinLeft.exchange.entrySet())
//            {
//                if (entry.getKey().equals(selectCoinRight.id+""))
//                {
//                    Log.e("TAG","============getHuiLv============="+entry.getValue());
//                    currHuiLv = Double.parseDouble(entry.getValue()+"");
//                    return currHuiLv;
//                }
//            }
//        }
//        currHuiLv = 0;
//        return 0;
//    }
//}